<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- header  -->
        <div>
          <span class="key-words">学科名称</span>
          <el-input
            class="inp"
            placeholder="请输入内容"
            v-model="subjectName"
          ></el-input>
          <el-button>清除</el-button>
          <el-button type="primary" @click="initData">搜索</el-button>
          <el-button type="success" class="btn" @click="open">
            <i class="el-icon-edit"></i>
            <span>新增试题</span>
          </el-button>
        </div>
        <!-- counts -->
        <div class="gray">
          <p class="font">
            <i class="el-icon-info"></i>
            <span
              >数据一共
              <span>{{ counts }}</span>
              条
            </span>
          </p>
        </div>
        <!-- table -->
        <el-table :data="list" style="width: 100%">
          <el-table-column
            type="index"
            label="序号"
            width="50"
          ></el-table-column>
          <el-table-column
            prop="subjectName"
            label="学科名称"
            width="120"
          ></el-table-column>
          <el-table-column prop="username" label="创建者" width="140">
          </el-table-column>
          <el-table-column prop="addDate" label="创建日期" width="200">
                 <template v-slot="{ row }">
              <span>{{timeCycle(row.addDate)}}</span>
          </template>
          </el-table-column>
          <el-table-column
            :formatter="formatter"
            v-model="isFrontDisplay"
            prop="isFrontDisplay"
            label="前台是否显示"
            width="150"
          >
          </el-table-column>
          <el-table-column
            prop="twoLevelDirectory"
            label="二级目录"
            width="120"
          >
          </el-table-column>
          <el-table-column prop="tags" label="标签" width="120">
          </el-table-column>
          <el-table-column prop="totals" label="题目数量" width="120">
          </el-table-column>

          <el-table-column label="操作" width="0">
            <template #default="{ row }">
              <el-link
                type="primary"
                :underline="false"
                style="margin-right: 10px"
                >学科分类</el-link
              >
              <el-link
                type="primary"
                :underline="false"
                style="margin-right: 10px"
                >学科标签</el-link
              >

              <el-link
                type="primary"
                :underline="false"
                style="margin-right: 10px"
                @click="editData(row.id)"
                >修改</el-link
              >

              <el-link
              @click="del(row.id)"
                type="primary"
                :underline="false"
                style="margin-right: 10px"
                >删除</el-link
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          class="el-pagination"
          background
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :current-page="page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pagesize"
          layout="  prev, pager, next,sizes, jumper"
          :total="counts"
        >
        </el-pagination>
        <!-- 新增表单 -->
        <el-dialog
          :title='title'
          :visible.sync="dialogFormVisible"
          width="400px"
          @close="close"
        >
          <el-form ref="form"  :rules="rules" :model="form">
            <el-form-item label="学科名称" prop="subjectName">
              <el-input
                autocomplete="off"
                style="width: 290px"
                placeholder="请输入学科名称"
                v-model="form.subjectName"
              ></el-input>
            </el-form-item>
            <el-form-item label="是否显示">
              <el-switch v-model="form.delivery"></el-switch>
            </el-form-item>
          </el-form>
          <div slot="footer">
            <el-button @click="close">取 消</el-button>
            <el-button type="primary" @click="addData">确 定</el-button>
          </div>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
// 学科分类和标签还没写
import { list, add, detail, update, remove } from '@/api/hmmm/subjects'
export default {
  data () {
    return {
      page: 1,
      pagesize: 10,
      counts: 0,
      isFrontDisplay: '',
      dialogFormVisible: false,
      subjectName: '',
      title: '新增学科',
      id: '',
      form: {
        subjectName: '',
        delivery: true
      },
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      },
      list: [] // 从接口处取回
    }
  },
  created () {
    this.listData()
    this.handleCurrentChange()
  },
  methods: {
    // 获取list
    async listData () {
      const res = await list({
        page: this.page,
        pagesize: this.pagesize
      })
      // console.log(res.data)
      this.counts = res.data.counts
      this.list = res.data.items
    },
    // 页面切换渲染
    handleCurrentChange (val) {
      // console.log(val)
      this.page = val
      this.listData()
    },
    // 修改条数渲染
    handleSizeChange (val) {
      console.log(val)
      this.pagesize = val
      console.log(this.pagesize)
      this.listData()
    },
    // 时间转换
    timeCycle (time) {
      return moment().format('YYYY-DD-MM hh:mm:ss')
    },
    // 渲染前台是否显示
    formatter (row) {
      // console.log(row)
      return row.isFrontDisplay ? '是' : '否'
    },
    // 搜索
    async initData () {
      const { data } = await list({
        subjectName: this.subjectName,
        page: this.page,
        pageSize: this.pageSize
      })
      console.log(data)
      this.list = data.items
      this.counts = data.counts
    },
    // 弹框
    open () {
      this.dialogFormVisible = true
    },
    // 关闭
    close () {
      // 清空
      this.$refs.form.resetFields()
      this.form = {
        subjectName: '',
        delivery: true
      }
      this.dialogFormVisible = false
    },
    // 添加
    async addData () {
      // 校验表单
      this.$refs.form.validate(async (isOk) => {
        if (this.title === '新增学科') {
          await add({
            subjectName: this.form.subjectName,
            isFrontDisplay: this.form.delivery ? 1 : 0
          })
        } else {
          await update({
            id: this.id,
            subjectName: this.form.subjectName,
            isFrontDisplay: this.form.delivery ? 1 : 0
          })
        }
      })
      this.listData()
      this.close()
    },
    // 编辑
    async editData (id) {
      // console.log(id)
      this.id = id
      this.title = '修改学科'
      const { data } = await detail({
        id
      })
      // console.log(data)
      this.form.subjectName = data.subjectName
      this.form.delivery = !!data.isFrontDisplay
      this.open()
    },
    // 删除
    async del (id) {
      await remove({ id })
      this.listData()
    }

  }
}
</script>

<style scoped>
.key-words {
  font-weight: 700;
  margin-right: 10px;
  margin-left: 15px;
}
.inp {
  display: inline-block;
  width: 20%;
  margin-right: 10px;
}
.gray {
  background-color: #f4f4f5;
  height: 30px;
  line-height: 30px;
  border-radius: 5px;
}
.font {
  font-size: 13px;
  margin: 10px;
  color: #909399;
}
.el-icon-info {
  margin-right: 10px;
}
.btn {
  display: flex;
  float: right;
}
.el-pagination {
  display: flex;
  float: right;
  margin: 20px;
}
</style>
